package com.stevenmoon.csdnplus.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.stevenmoon.csdnplus.R;
import com.stevenmoon.csdnplus.activity.HeadlineDetailActivity;
import com.stevenmoon.csdnplus.adapter.HeadlineLatestAdapter;
import com.stevenmoon.csdnplus.bean.headline.HeadlineInfoBean;
import com.stevenmoon.csdnplus.event.HeadlineTabReselectedEvent;
import com.stevenmoon.csdnplus.fragment.base.LazyBaseFragment;
import com.stevenmoon.csdnplus.mvpview.RefreshLoadMoreView;
import com.stevenmoon.csdnplus.presenter.HeadlineLatestPresenter;
import com.stevenmoon.csdnplus.util.TipUtil;
import com.stevenmoon.csdnplus.widget.decorator.EasyBorderDividerItemDecoration;

import org.simple.eventbus.Subscriber;

import java.util.List;

import butterknife.Bind;

/**
 * Created by WangJun on 2016/4/12.
 */
public class HeadlineLatestListFragment extends LazyBaseFragment<HeadlineLatestListFragment, HeadlineLatestPresenter>
        implements RefreshLoadMoreView<List<HeadlineInfoBean>>, HeadlineLatestAdapter.OnHeadlineItemClickListener {


    @Bind(R.id.dataRV)
    RecyclerView mDataRV;
    @Bind(R.id.refreshLayout)
    MaterialRefreshLayout mRefreshLayout;

    HeadlineLatestAdapter mAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initViews() {
        mDataRV.addItemDecoration(
                new EasyBorderDividerItemDecoration(getResources().getDimensionPixelSize(R.dimen.spacing_small),
                        getResources().getDimensionPixelSize(R.dimen.spacing_small)));
        mDataRV.setLayoutManager(new LinearLayoutManager(getActivity()));
        mDataRV.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                switch (newState) {
                    case RecyclerView.SCROLL_STATE_IDLE:
                        Glide.with(getActivity()).resumeRequests();
                        break;
                    case RecyclerView.SCROLL_STATE_DRAGGING:
                        Glide.with(getActivity()).pauseRequests();
                        break;
                    case RecyclerView.SCROLL_STATE_SETTLING:
                        Glide.with(getActivity()).pauseRequests();
                        break;

                }
            }
        });
        mAdapter = new HeadlineLatestAdapter(getActivity(), null, this);
        mDataRV.setAdapter(mAdapter);
        mRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
                presenter.loadNewData();
            }

            @Override
            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                super.onRefreshLoadMore(materialRefreshLayout);
                presenter.loadMoreData(mAdapter.getLastItem().get_id());

            }
        });

    }

    @Override
    public void onFirstUserVisible() {
        super.onFirstUserVisible();
        mRefreshLayout.autoRefresh();
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_headline_latest;
    }

    @Override
    public HeadlineLatestPresenter createPresenter() {
        return new HeadlineLatestPresenter();
    }

    @Override
    public void loadMoreDataSuccess(List<HeadlineInfoBean> data) {
        TipUtil.showToast("加载更多成功");
        mRefreshLayout.finishRefreshLoadMore();
        mAdapter.addAll(data);
    }

    @Override
    public void loadMoreDataFail(String msg) {
        mRefreshLayout.finishRefreshLoadMore();
        TipUtil.showToast(msg);
    }

    @Override
    public void showNoMoreData() {
        mRefreshLayout.finishRefreshLoadMore();
        TipUtil.showToast("厉害厉害，都被你看光了，满足了吧o(*￣︶￣*)o");
    }

    @Override
    public void showLoading() {
        //TODO 需要区分自动下拉刷新与加载loadingView的情况
    }

    @Override
    public void hideLoading() {
        mRefreshLayout.finishRefresh();
    }

    @Override
    public void loadNewDataSuccess(List<HeadlineInfoBean> data) {
        mAdapter.setData(data);
    }

    @Override
    public void loadNewDataFail(String msg) {
        mRefreshLayout.finishRefresh();
        TipUtil.showSnack(getActivity(), msg);
    }

    @Override
    public void OnHeadlineItemClick(HeadlineInfoBean item) {
        HeadlineDetailActivity.showDetail(getActivity(), item.getUser_name(), item.getId(), item.getUrl(), item.getType());
    }

    @Subscriber
    void tabReselected(HeadlineTabReselectedEvent event){
        if(event.getCommId() == 0){
            mDataRV.smoothScrollToPosition(0);
        }
    }

}
